<?php
/*********************************************
 *  CPG Dragonfly™ CMS
 *********************************************
	Copyright © since 2010 by CPG-Nuke Dev Team
	http://dragonflycms.org

	Dragonfly is released under the terms and conditions
	of the GNU GPL version 2 or any later version
*/

namespace Poodle\Input;

class GET extends \Poodle\Input
{
	# ArrayAccess
	public function offsetGet($k) { return ($this->offsetExists($k) ? parent::offsetGet($k) : null); }

	# Poodle
	public function keys () { return array_keys(func_num_args() ? self::_get(func_get_args()) : $this->getArrayCopy()); }
	public function exist() { return !is_null(self::_get(func_get_args())); }
	public function bit  () { return !self::_get(func_get_args())?0:1; }
	public function bool () { $v = self::_get(func_get_args()); return ('false'!==$v && !empty($v)); }
	public function float() { return self::str2float(self::_get(func_get_args())); }
	public function int  () { $v = self::_get(func_get_args()); return (preg_match('#^-?\d+$#', $v) ? (int)$v : null); }
	public function map  () {
		$c = get_class($this);
		$v = self::_get(func_get_args());
		return is_array($v) ? new $c($v) : null;
	}
	public function raw  () { return self::_get(func_get_args()); }
	public function txt  () { return self::strip(self::_get(func_get_args())); }
	public function text () { return self::strip(self::_get(func_get_args())); }
	public function uint () { $v = self::_get(func_get_args()); return (ctype_digit((string)$v) ? (int)$v : null); }

	protected function _get($args)
	{
		if (!$args) { return null; }
		$c = count($args);
		$v = $this;
		for ($i=0; $i<$c; ++$i)
		{
			$k = $args[$i];
			if (!is_string($k) && !is_int($k)) { throw new \Exception("Parameter {$i} is not a string. Type is: ".gettype($k)); }
			if (!isset($v[$k])) { return null; }
			$v = $v[$k];
		}
		return $v;
	}

	public function __toString() { return print_r($this, true); }
}
